/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poker.cards;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 *
 * @author Norteromius
 */
public class Card implements Comparable<Card>
{

    private String kind;
    private String color;
    private int countInColor; // mierzone w zakresie 0 - 3
    private int countInKind; // mierzone w zakresie 2 - 14
    //private int count;        // mierzone w zakresie 0 - 51
    private BufferedImage img = null;

    public Card(int num)
    {
        if (num > 51)
        {
            System.out.println("Za duzo !!!!!!");
            return;
        } else
        {
          //  count = num;
            int xkind = (num % 13) + 2;
            setCards(num / 13, xkind, false);
        }
    }

    /**
     * Konstruktor karty
     * @param color
     * @param count 
     */
    public Card(int color, int kind)
    {
        //count = color * 13 + kind-2;
        //System.out.println("co " + count);
        setCards(color, kind, true);
    }

    private void setCards(int color, int kind, boolean loadimage)
    {
        
        String name = "../resources/";

        switch (kind)
        {
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                this.kind = kind + "";
                break;
            case 11:
                this.kind = CardsConstants.KEY_TYPE_11;
                break;
            case 12:
                this.kind = CardsConstants.KEY_TYPE_12;
                break;
            case 13:
                this.kind = CardsConstants.KEY_TYPE_13;
                break;
            case 14:
                this.kind = CardsConstants.KEY_TYPE_14;
                break;
        }

        switch (color)
        {
            case 0:
                this.color = CardsConstants.KEY_COLOR_SPADE;
                name += "s";
                break;
            case 1:
                this.color = CardsConstants.KEY_COLOR_CLUB;
                name += "c";
                break;
            case 2:
                this.color = CardsConstants.KEY_COLOR_HEART;
                name += "h";
                break;
            case 3:
                this.color = CardsConstants.KEY_COLOR_DIAMOND;
                name += "d";
                break;
        }

        this.countInColor = color;
        this.countInKind = kind;

        name += kind + ".png";

        if (loadimage)
        {
            try
            {
                img = ImageIO.read(this.getClass().getResourceAsStream(name));
            } catch (IOException e)
            {
                Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, e);
            }
        }
    }

    public ImageIcon getImage()
    {
        return new ImageIcon(img);
    }

    @Override
    public int compareTo(Card o)
    {
        //return countInColor < o.countInColor ? -1 : (countInColor == o.countInColor ? 0 : 1);
        return countInKind < o.countInKind ? -1 : (countInKind == o.countInKind ? 0 : 1);
        //return count < o.count ? -1 : (count == o.count ? 0 : 1);
        /*if (countInColor == o.countInColor && countInKind == o.countInKind)
        return 0;
        else
        {
        if (countInColor < o.countInColor || countInKind < o.countInKind)
        return -1;
        else
        return 1;
        }*/
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (!(o instanceof Card))
        {
            return false;
        }

        Card oo = (Card) o;

        if (countInColor == oo.countInColor && countInKind == oo.countInKind)
        //if (count == oo.count)
        {
            return true;
        } else
        {
            return false;
        }
    }

    @Override
    public int hashCode()
    {
        int hash = 7;
        hash = 61 * hash + this.countInColor;
        hash = 61 * hash + this.countInKind;
        return hash;
    }

    /**
     * 
     * @return 
     */
    @Override
    public String toString()
    {
        return kind + "-" + color;
    }

    public String getColor()
    {
        return color;
    }

    /**
     * 
     * @param color 
     */
    public void setColor(String color)
    {
        this.color = color;
    }

    /**
     * sprawdza, ktora dana karta jest w kolejności
     * @return 
     */
    public int getCountInKind()
    {
        return countInKind;
    }

    public void setCountInKind(int countInKolor)
    {
        this.countInKind = countInKolor;
    }

    public String getKind()
    {
        return kind;
    }

    public void setKind(String kind)
    {
        this.kind = kind;
    }
}
